package cn.yeziji.forum.basic.file;

import cn.yzj.utils.DataUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.Date;

/**
 * webuploader 数据
 *
 * @author gzkemays
 * @since 2022/5/16 9:01
 */
@Data
@NoArgsConstructor
public class WebUploader implements Serializable {
  /** 上传 webupload id */
  String id;
  /** 文件名称 */
  String name;
  /** 文件类型 */
  String type;
  /** 文件 MD5 */
  String md5;
  /** 文件父级目录 */
  String path;
  /** 上传码 */
  String uploadCode;
  /** 文件大小 */
  Long size;
  /** 当前分片 */
  Integer chunk;
  /** 总分片数 */
  Integer chunks;
  /** 最后修改的时间 */
  Date lastModifiedDate;
  /** 上传的文件 */
  MultipartFile file;
  /** 文件描述 */
  String desc;

  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  public static class ChuckEntity implements Serializable {
    String uploaderMd5;
    Integer chunks;
    Integer chunk;
    String username;
    String path;
    Long size;
    String originalFilename;
    String desc;
    String key;

    public ChuckEntity(String key) {
      String md5 = DataUtils.substrBeforeOfTag(key, "-");
      Integer chunks = Integer.parseInt(DataUtils.substrBetweenOfFirstAndFirst(key, "-", "->"));
      String username = DataUtils.substrBetweenOfFirstAndLast(key, "_", "#");
      String path = DataUtils.substrBetweenOfLastAndLast(key, "#", "&");
      long size = Long.parseLong(DataUtils.substrBetweenOfFirstAndLast(key, "&", "$"));
      String filename = DataUtils.substrAfterOfLastTag(key, "$");
    }

    public ChuckEntity(
        String uploaderMd5,
        Integer chunks,
        Integer chunk,
        String username,
        String path,
        Long size,
        String originalFilename,
        String desc) {
      this.uploaderMd5 = uploaderMd5;
      this.chunks = chunks;
      this.chunk = chunk;
      this.username = username;
      this.path = path;
      this.size = size;
      this.originalFilename = originalFilename;
      this.desc = desc;
      this.key =
          uploaderMd5
              + "-"
              + (chunks - 1)
              + "->"
              + chunk
              + "_"
              + username
              + "#"
              + path
              + "&"
              + size
              + "$"
              + originalFilename
              + "~"
              + desc;
    }
  }
}
